<html>
	<head></head>
	<body>
		<script>
			var apikey="73345b0dfb090b49c300b0199de0bae762f70d70cf47871a6";
			function suggestion(callback, word) {
				var xhr = new XMLHttpRequest();
				var apiuri="http://api.wordnik.com/v4/word.json/";
				
				xhr.onreadystatechange = function(data) {
					if (xhr.readyState == 4) {
						var data = JSON.parse(xhr.responseText);
						callback(data);
					}
				}
				xhr.open("GET", apiuri+word, true);
				xhr.setRequestHeader("api_key", apikey);
				xhr.send();
			};

			function query(callback, word) {
				var xhr = new XMLHttpRequest();
				var apiuri="http://api.wordnik.com/v4/word.json/";
				
				xhr.onreadystatechange = function(data) {
					if (xhr.readyState == 4) {
						var data = JSON.parse(xhr.responseText);
						callback(data);
					}
				}
				xhr.open("GET", apiuri+word+"/pronunciations?useCanonical=true", true);
				xhr.setRequestHeader("api_key", apikey);
				xhr.send();
			};


			chrome.extension.onRequest.addListener(
				function (request, sender, callback) {
					if (request.action == 'suggestion') {
						suggestion(callback, request.word);
					}
					else if (request.action == 'query') {
						query(callback, request.word);
					}
				}
			);

			function genericOnClick(info, tab) {
				chrome.tabs.executeScript(tab.id, {"file": "injection.js"});
			}

			var id = chrome.contextMenus.create({
				"title": "How to pronounce for '%s'",
				"contexts": ["selection"],
				"onclick": genericOnClick});
		</script>
	</body>
</html>
